﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Uco.Models;
using Uco.Infrastructure;
using System.Globalization;

namespace Uco.Controllers
{
    public class PageController : BasePageController
    {
        #region Pages

        public ActionResult LanguagePage(string name)
        {
            return View(CurrentPage);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ContentPage(string name)
        {
            return View(CurrentPage);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult SiteMapPage(string name, string xml)
        {
            if (string.IsNullOrEmpty(xml) || xml != "1")
            {
                ViewBag.SiteMapData = SF.GetSiteMapFormated();
                return View(CurrentPage);
            }
            else
            {
                HttpContext.Response.Clear();
                return Content(SF.GetSiteMapFormatedXML(), "text/xml");
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Form(string name)
        {
            return View(CurrentPage);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Form(ContactForm cf, string InvisibleCaptchaValue)
        {
            if (!CaptchaController.IsInvisibleCaptchaValid(InvisibleCaptchaValue))
            {
                ModelState.AddModelError(string.Empty, "Captcha error.");
                return Content("Error: Captcha", "text/html");
            }

            if (ModelState.IsValid)
            {
                Contact c = new Contact();

                c.ContactName = cf.ContactName;
                c.ContactEmail = cf.ContactEmail;
                c.ContactPhone = cf.ContactPhone;
                c.ContactData = cf.ContactData;

                c.ContactReferal = SF.GetCookie("Referal");
                c.ContactUrl = "<a target='_blank' href='http://" + Request.ServerVariables["HTTP_HOST"] + Url.Content(CurrentPage.Url) + "'>" + CurrentPage.Title + "</a>";
                c.ContactDate = DateTime.Now;

                List<Contact> cl = SF.LoadContacts();
                if (cl.Count == 0) c.ID = 1;
                else c.ID = cl.Max(r => r.ID) + 1;
                cl.Add(c);
                SF.SaveContacts(cl);

                return Content(((Form)CurrentPage).FormTextAfter, "text/html");
            }
            else
            {
                return View(cf);
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ArticleAll(string name)
        {
            List<Article> l = new List<Article>();
            foreach(UcoMenuItem item in SF.GetMenuRepository().Where(r => r.ParentID == CurrentPage.ID))
            {
                if (item.RouteUrl == "ar") l.Add((Article)SF.LoadPage(item.SeoUrlName));
            }

            ViewBag.Items = l;

            return View(CurrentPage);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Article(string name)
        {
            return View(CurrentPage);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult GalleryAll(string name)
        {
            return View(CurrentPage);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult NewsAllPage(string name)
        {
            return View(CurrentPage);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult NewsPage(string name)
        {
            return View(CurrentPage);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult RedirectPage(string name)
        {
            return Redirect(((RedirectPage)CurrentPage).RedirectTo);
        }

        #endregion


    }
}
